<?php

/**
 * This is the model class for table "content".
 *
 * The followings are the available columns in table 'content':
 * @property integer $content_id
 * @property string $author
 * @property string $url
 * @property integer $is_video
 * @property string $title
 * @property string $tags
 * @property string $description
 * @property integer $is_approved
 * @property string $date_added
 * @property string $date_approved
 * @property string $file
 *
 * The followings are the available model relations:
 * @property Users $author0
 */
class Content extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Content the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'content';
    }

    public function CorrectImageVideoValidator($attribute, $params) {
        fb::log("CorrectImageVideoValidator");
        fb::log($attribute);
        fb::log($params);
        fb::log($this->file, "File");
        fb::log($this->url, "URL");
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('description', 'length', 'max' => 200),
            array('is_video', 'required', 'message' => 'Musisz wybrać typ pliku!'),
            array('is_video', 'in', 'range' => array('0', '1'), 'message' => 'Musisz wybrać typ pliku!'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('url, file, title, tags, description', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'author0' => array(self::BELONGS_TO, 'Users', 'author'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'content_id' => 'Content',
            'author' => 'Autor',
            'url' => 'URL',
            'is_video' => 'Video?',
            'title' => 'Tytuł',
            'tags' => 'Tagi',
            'description' => 'Opis',
            'is_approved' => 'Zatwierdzony?',
            'date_added' => 'Data Dodania',
            'date_approved' => 'Data zatwierdzenia',
            'file' => 'Plik Obrazu',
            'source' => 'Źródło',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('content_id', $this->content_id);
        $criteria->compare('author', $this->author, true);
        $criteria->compare('url', $this->url, true);
        $criteria->compare('is_video', $this->is_video);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('tags', $this->tags, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('is_approved', $this->is_approved);
        $criteria->compare('date_added', $this->date_added, true);
        $criteria->compare('date_approved', $this->date_approved, true);
        $criteria->compare('file', $this->file, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function beforeSave() {
        if ($this->isNewRecord) {
            $this->author = Yii::app()->user->id;
            $this->tags = '';
            $this->date_added = new CDbExpression('NOW()');
            $this->date_approved = new CDbExpression('NULL');
        }
        return parent::beforeSave();
    }

}